
package com.kugou.game.sdk.ui.activity;

import com.kugou.framework.base.LogUtil;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.MyMessage;
import com.kugou.game.sdk.interfaces.OnEditMessageListener;
import com.kugou.game.sdk.interfaces.OnLoadMessageInfoListener;
import com.kugou.game.sdk.ui.widget.TipsLayout;
import com.kugou.game.sdk.utils.Rx;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 描述:消息详情页面
 * 
 * @author liux
 * @since 2013-12-25 下午02:46:00
 */
public class MessageDetailActivity extends BaseCommonTitleFragmentActivity {

    private static final int MSG_REQUEST_MESSAGE_DETAIL = 0;

    private static final int MSG_SHOW_LOADING_VIEW = 1;

    protected static final int MSG_REQUEST_MESSAGE_DETAIL_SUCCESS = 2;

    protected static final int MSG_REQUEST_MESSAGE_DETAIL_FAILE = 3;

    private static final int MSG_DELE_MSG = 4;

    private static final int MSG_DELE_MSG_SUCCESS = 5;

    private static final int MSG_DELE_MSG_FAILE = 6;

    /** 设置信息已读 */
    private static final int MSG_SET_MSG_RED = 7;

    private LinearLayout mMsgLayout;

    private TextView mMsgTitle;

    private TextView mMsgTime;

    private TextView mMsgContent;

    private TipsLayout mTipsLayout;

    private int msgId;

    private int msgPosition;

    private int isRead;

    private int pageIndex;

    private boolean editState = false;// 默认为非编辑状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = Rx.layout.kg_activity_message_detail;
        setContentView(layoutId);
        // 设置标题
        setCommonTitle(Rx.string.kg_kugou_mail_message_activity);
        // 初始化界面
        initViews();
        msgId = getIntent().getIntExtra(MSG_ID, 0);
        msgPosition = getIntent().getIntExtra(MSG_POSITION, 0);
        isRead = getIntent().getIntExtra("isread", 0);
        pageIndex = getIntent().getIntExtra("pageIndex", 0);
        sendEmptyBackgroundMessage(MSG_REQUEST_MESSAGE_DETAIL);
    }

    /**
     * 初始化界面控件
     */
    private void initViews() {
        // 消息内容
        mMsgLayout = (LinearLayout) findViewById(Rx.id.kg_message_detail_layout);
        mMsgTitle = (TextView) findViewById(Rx.id.kg_msg_title);
        mMsgTime = (TextView) findViewById(Rx.id.kg_msg_time);
        mMsgContent = (TextView) findViewById(Rx.id.kg_msg_text);

        mTipsLayout = (TipsLayout) findViewById(Rx.id.kg_tips_layout);
        mTipsLayout.setLoadingText("");
        mTipsLayout.setLoadingViewBackground(Rx.drawable.kg_gamecenter_loading);
        mTipsLayout.setOnRefreshButtonClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                sendEmptyBackgroundMessage(MSG_REQUEST_MESSAGE_DETAIL);
            }
        });
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        User user = UserManager.getInstance().getCurrentUserInternal();
        if (user == null) {
            showToast("CurrentUser == null");
            return;
        }
        final Message message = new Message();
        switch (msg.what) {
            case MSG_REQUEST_MESSAGE_DETAIL:// 请求消息内容
                sendEmptyUiMessage(MSG_SHOW_LOADING_VIEW);
                UserManager.getInstance().getMessageInfo(user.getAccountName(), msgId,
                        new OnLoadMessageInfoListener() {
                            @Override
                            public void onSuccess(MyMessage myMsg) {
                                message.what = MSG_REQUEST_MESSAGE_DETAIL_SUCCESS;
                                message.obj = myMsg;
                            }

                            @Override
                            public void onFailed(String errorMsg) {
                                message.what = MSG_REQUEST_MESSAGE_DETAIL_FAILE;
                                message.obj = errorMsg;
                            }
                        });

                break;
            case MSG_SET_MSG_RED:
                // 通知服务器刷新数据
                long appId1 = SDKControler.getAppId();
                String pwd1 = null;
                int source = 0;
                if (TextUtils.isEmpty(user.getPassword())) {
                    pwd1 = user.getKugouToken();
                    source = 1;
                } else {
                    pwd1 = user.getPassword();
                    source = 0;
                }
                if (msg.obj != null && msg.obj instanceof MyMessage) {
                    MyMessage myMsg = (MyMessage) msg.obj;
                    UserManager.getInstance().editMessageItem(user.getAccountName(), "readmessage",
                            myMsg.getMessageid() + "", myMsg.getPageIndex(),
                            MessageListActivity.PAGE_SIZE, new OnEditMessageListener() {
                                @Override
                                public void onSuccess() {
                                    LogUtil.d("成功");
                                }

                                @Override
                                public void onFailed(String errorMsg) {
                                    LogUtil.d("失败");

                                }
                            });
                }
                break;
        }
        sendUiMessage(message);
    }

    @Override
    public void handleUiMessage(Message msg) {
        switch (msg.what) {
            case MSG_SHOW_LOADING_VIEW:// 显示正在加载页面
                mTipsLayout.setVisibility(View.VISIBLE);
                mTipsLayout.show(TipsLayout.TYPE_LOADING);
                mMsgLayout.setVisibility(View.GONE);
                break;
            case MSG_REQUEST_MESSAGE_DETAIL_SUCCESS:// 获取数据成功
                mTipsLayout.setVisibility(View.GONE);
                mMsgLayout.setVisibility(View.VISIBLE);

                MyMessage myMsg = (MyMessage) msg.obj;
                myMsg.setPageIndex(pageIndex);
                showMsgContent(myMsg);

                if (isRead == 0) {
                    Intent broadcastIntent = new Intent(SDKActions.ACTION_READ_ONE_EMAIL);
                    broadcastIntent.putExtra(MSG_ID, myMsg.getMessageid());
                    broadcastIntent.putExtra(MSG_POSITION, msgPosition);
                    sendBroadcast(broadcastIntent);
                    // 通知服务器刷新数据
                    Message message = new Message();
                    message.what = MSG_SET_MSG_RED;
                    message.obj = myMsg;
                    sendBackgroundMessage(message);
                }
                break;
            case MSG_REQUEST_MESSAGE_DETAIL_FAILE:// 获取数据失败
                mTipsLayout.setVisibility(View.VISIBLE);
                mTipsLayout.show(TipsLayout.TYPE_FAILE);
                mMsgLayout.setVisibility(View.GONE);

                String errorMsg = (String) msg.obj;
                showToast(errorMsg);
                break;
            case MSG_DELE_MSG_SUCCESS:// 删除数据成功
                Intent intent = new Intent();
                intent.putExtra(MSG_ID, msgId);
                intent.putExtra(MSG_POSITION, msgPosition);
                setResult(RESULT_OK, intent);

                finish();
                break;
            case MSG_DELE_MSG_FAILE:// 删除数据失败
                mTipsLayout.setVisibility(View.GONE);
                mMsgLayout.setVisibility(View.VISIBLE);

                String error = (String) msg.obj;
                showToast(error);
                break;
        }
    }

    /**
     * 显示消息内容
     */
    private void showMsgContent(MyMessage myMsg) {
        if (myMsg != null) {
            mMsgTitle.setText(myMsg.getTitle());
            mMsgTime.setText(myMsg.getInserttime());
            String content = myMsg.getContent();
            if (!TextUtils.isEmpty(content)) {
                mMsgContent.setText(Html.fromHtml(myMsg.getContent()));
            } else {
                mMsgContent.setText("");
            }

        } else {
            showToast("没有消息内容");
        }
    }
}
